package com.s7.app.delegate;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;

/**
 * Created by Administor on 2018\7\16 0016.
 * View delegate base class
 * @author s7
 * Activity 视图层代理的基类
 */
public abstract class BaseADelegate implements ADelegate {

    /**
     * 存储View
     */
    protected final SparseArray<View> mViews = new SparseArray<View>();

    /**
     * 获取根布局
     * @return
     */
    public abstract int getLayoutId();

    protected Context mContext;

    public View rootView;

    public String getString(int resId){
        return mContext.getString(resId);
    }

    public Resources getResources(){
        return mContext.getResources();
    }

    public Context getContext(){
        return mContext;
    }

    @Override
    public void onCreate(LayoutInflater inflater, Bundle savedInstanceState) {
        mContext = inflater.getContext();
        rootView = inflater.inflate(getLayoutId(), null, false);
    }

    @Override
    public int getOptionsMenuId() {
        return 0;
    }

    @Override
    public View getRootView() {
        return rootView;
    }

    @Override
    public void initWidget() {

    }

    public <T extends View> T findViewById(int id) {
        T view = (T) mViews.get(id);
        if (view == null) {
            view = rootView.findViewById(id);
            mViews.put(id, view);
        }
        return view;
    }
}

